package com.itheima.edu.info.manager.controller;

import com.itheima.edu.info.manager.domain.Student;
import com.itheima.edu.info.manager.service.StudentService;

import java.util.Scanner;

public class StudentController {
    private Scanner sc = new Scanner(System.in);
    private StudentService studentService = new StudentService();

    public void start() {
        Scanner sc = new Scanner(System.in);
        stu:
        while (true) {
            System.out.println("--------欢迎来到 <学生> 管理系统--------");
            System.out.println("请输入您的选择: 1.添加学生  2.删除学生  3.修改学生  4.查看学生  5.退出");
            int choice = sc.nextInt();
            switch (choice) {
                case 1:
                    System.out.println("添加学生");
                    addStudent();
                    break;
                case 2:
                    System.out.println("删除学生");
                    deleteStudent();
                    break;
                case 3:
                    System.out.println("修改学生");
                    updateStudent();
                    break;
                case 4:
                    System.out.println("查看学生");
                    findStudent();
                    break;
                case 5:
                    System.out.println("退出学生管理系统");
                    break stu;
                default:
                    System.out.println("输入有误,请重新输入");
                    break;
            }
        }
    }//分菜单表头  增删改查

    public void deleteStudent() {
        String id;
        id = sc.next();
        studentService.deleteId(id);
        System.out.println("删除成功");
    }//删除学生信息


    public void findStudent() {//查找学生信息
        //调用service，传回一个数组
        Student[] stu3 = StudentService.findStu();
        if (stu3 == null) { //判断数组内存地址不为空
            System.out.println("查无信息，请输入后再试");
            return;
        }
        System.out.println("学号\t\t姓名\t\t年龄\t\t生日");
        for (int i = 0; i < stu3.length; i++) {
            Student arr = stu3[i];
            if (arr != null) {          //[ 4,null,null,null]注意
                System.out.println(arr.getId() + "\t\t" + arr.getName() + "\t\t" + arr.getAge() + "\t\t" + arr.getBirthday());
            }
        } //查找学生信息 done
    }

    public void updateStudent() {
        String id;
        while(true) {
            System.out.println("请输入要修改的学号");
            id = sc.next();
            boolean flag = studentService.isExists(id);
            if (flag) {

                System.out.println("请输入修改后的姓名");
                String name = sc.next();
                System.out.println("请输入修改后的年龄");
                String age = sc.next();
                System.out.println("请输入修改后的生日");
                String birthday = sc.next();
                studentService.updateStudent(id,name,age,birthday);
                System.out.println("修改成功");

                break;
            } else {
                System.out.println("查无信息,请输入后再试");
            }
        }


    }
        //修改学生信息


        public void addStudent () {                   //判断是否存在
            String id;
            while (true) {
                System.out.println("请录入学生id");
                id = sc.next();
                boolean flag = StudentService.isExists(id);
                if (flag) {
                    System.out.println("学号存在，请重新输入");
                } else {
                    break;
                }                                        //存在判断结束
            }
            System.out.println("请录入学生姓名");
            String name = sc.next();
            System.out.println("请录入学生年龄");
            String age = sc.next();
            System.out.println("请录入学生生日");
            String birthday = sc.next();
            Student stu = new Student();
            stu.setId(id);
            stu.setName(name);
            stu.setAge(age);
            stu.setBirthday(birthday);
            //信息封装好了之后传给业务员,此时建立业务员对象
            boolean flag = studentService.addStudent(stu);//给出一个返回值告知是否添加成功,
            if (flag) {
                System.out.println("添加成功");
            } else {
                System.out.println("添加失败");
            }
        }//添加学生信息 done
    }
